﻿<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<title>Client-Server Sync Tests</title>
		
		<link href="../Common/Styles/qunit.css" type="text/css" rel="stylesheet" />
		
		<script src="../Common/Scripts/JQuery/jquery-1.3.2.js" type="text/javascript"></script>
		<script src="../Common/Scripts/QUnit/qunit.js" type="text/javascript"></script>
		<script src="../Common/Scripts/Microsoft/MicrosoftAjax.js" type="text/javascript"></script>
		<script src="../Common/Scripts/ExoWeb/exoweb.js" type="text/javascript"></script>
		<script src="../Common/Scripts/QUnit/qunit.ext.js" type="text/javascript"></script>
		<script src="../Common/Scripts/ExoWeb/exoweb.model.js" type="text/javascript"></script>
		<script src="../Common/Scripts/ExoWeb/exoweb.mapper.js" type="text/javascript"></script>
		<script src="../Common/Scripts/ExoWeb/exoweb.mock.js" type="text/javascript"></script>
		<script src="../Common/Scripts/mock-driver.js" type="text/javascript"></script>
		<script src="ChangeSet.js" type="text/javascript"></script>
		
		<script type="text/javascript">
			//ExoWeb.trace.flags.sync = true;
			//ExoWeb.trace.flags.mocks = true;
			ExoWeb.Mock.objectProviderDelay = 0;
			ExoWeb.Mock.typeProviderDelay = 0;
			ExoWeb.Mock.roundtripProviderDelay = 0;
			ExoWeb.Mock.simulateLazyLoading = false;

			// HACK: signal behavior that temporarily releases control conflicts with synchronous nature of tests
			var impl = ExoWeb.Signal.prototype._doCallback;
			ExoWeb.Signal.prototype._doCallback = function Signal$_doCallback(name, thisPtr, callback, args, executeImmediately) {
				impl.call(this, name, thisPtr, callback, args, true);
			};

			var context = ExoWeb.context({ model: { driver: { id: "1", from: "Driver" } } });

			setupTest("test1", { description: "Condition Types Available On Client", expect: 11 }, function() {
				ok(ExoWeb.Model.ConditionType.get("Person.NameRequired"), "Condition type should exist");
				ok(ExoWeb.Model.ConditionType.get("Person.NameLength"), "Condition type should exist");
				ok(ExoWeb.Model.ConditionType.get("Person.PhoneNumberRequired"), "Condition type should exist");
				ok(ExoWeb.Model.ConditionType.get("Driver.DealerRequired"), "Condition type should exist");
				ok(ExoWeb.Model.ConditionType.get("Driver.DealerAllowedValues"), "Condition type should exist");
				ok(ExoWeb.Model.ConditionType.get("Driver.CarsRequired"), "Condition type should exist");
				ok(ExoWeb.Model.ConditionType.get("Driver.CarsAllowedValues"), "Condition type should exist");
				ok(ExoWeb.Model.ConditionType.get("Driver.MilesDrivenRange"), "Condition type should exist");
				ok(ExoWeb.Model.ConditionType.get("Driver.AllowedSalesPerson"), "Condition type should exist");
				ok(ExoWeb.Model.ConditionType.get("Driver.NotesLength"), "Condition type should exist");
				ok(ExoWeb.Model.ConditionType.get("CarOwner.LocationAllowedValues"), "Condition type should exist");
			});

			setupTest("test2", { description: "Conditions Sent From Server" }, function() {
				var employee = Employee.meta.get("103");
				equals(employee.meta.conditions().length, 1, "Should have 1 condition from the server");
				employee.set_Name("Test");
				equals(employee.meta.conditions().length, 0, "Should have 0 conditions after setting value");
			});

			setupTest("test3", { description: "Conditions Caused by Rules on the Client" }, function() {
				function changeValue(obj, property, value, callback) {
					var original = ExoWeb.getValue(obj, property);
					Sys.Observer.setValue(obj, property, value);
					callback();
					Sys.Observer.setValue(obj, property, original);
				}

				changeValue(context.model.driver, "Dealer", null, function() {
					equals(context.model.driver.meta.conditions(Driver.$Dealer).length, 1, "Should have 1 condition related to the dealer");
					equals(context.model.driver.meta.conditions(Driver.$Dealer)[0].get_type(), ExoWeb.Model.ConditionType.get("Driver.DealerRequired"),
						"Condition type should be \"Driver.DealerRequired\"");
				});
				changeValue(context.model.driver, "MilesDriven", -5, function() {
					equals(context.model.driver.meta.conditions(Driver.$MilesDriven).length, 1, "Should have 1 condition related to the number of miles driven");
					equals(context.model.driver.meta.conditions(Driver.$MilesDriven)[0].get_type(), ExoWeb.Model.ConditionType.get("Driver.MilesDrivenRange"),
						"Condition type should be \"Driver.MilesDrivenRange\"");
				});

				equals(context.model.driver.meta.conditions().length, 0, "Should have 0 conditions");
			});

			setupTest("test4", { description: "Conditions Caused by Roundtrip", expect: 0 }, function() {
				
			});
			
			timeoutTests(5000);

			context.ready(function() {
				executeTest("test1");
				executeTest("test2");
				executeTest("test3");
				executeTest("test4");
			});
		</script>
	</head>
	<body xmlns:sys="javascript:Sys" xmlns:dataview="javascript:Sys.UI.DataView" sys:activate="*">
		
		<!-- QUnit Display -->
		<h1 id="qunit-header">Test Results:</h1>
		<h2 id="qunit-banner"></h2>
		<h2 id="qunit-userAgent"></h2>
		<ol id="qunit-tests"></ol>
	</body>
</html>
